#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H

#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonDocument>
#include <QString>

class NetworkManager : public QObject
{
    Q_OBJECT
public:
    enum RequestType {
        GET = 0,
        POST,
    };

public:
    NetworkManager(QObject *parent = nullptr);

    void checkExpiry();
    void checkStock();
    void loginUser(const QJsonObject &json);
    void registerUser(const QJsonObject &json);
    void deleteUser(const QJsonObject &json);
    void updateUser(const QJsonObject &json);
    void addMedicine(const QJsonObject &json);
    void deleteMedicine(const QJsonObject &json);
    void editMedicine(const QJsonObject &json);
    void queryMedicineByName(const QJsonObject &json);
    void queryMedicineByCategory(const QJsonObject &json);
    void queryMedicineByKeyword(const QJsonObject &json);
    void takeMedicine(const QJsonObject &json);
    void querySalesRecords(const QJsonObject &json);
    void createPurchaseOrder(const QJsonObject &json);
    void queryPurchaseRecord(const QJsonObject &json);

signals:
    void checkExpiryResponseReceived(const QJsonObject &response);
    void loginResponseReceived(const QJsonObject &response);
    void registerResponseReceived(const QJsonObject &response);
    void deleteUserResponseReceived(const QJsonObject &response);
    void updateUserResponseReceived(const QJsonObject &response);
    void addMedicineResponseReceived(const QJsonObject &response);
    void deleteMedicineResponseReceived(const QJsonObject &response);
    void editMedicineResponseReceived(const QJsonObject &response);
    void queryMedicineByNameResponseReceived(const QJsonObject &response);
    void queryMedicineByCategoryResponseReceived(const QJsonObject &response);
    void queryMedicineByKeywordResponseReceived(const QJsonObject &response);
    void checkStockResponseReceived(const QJsonObject &response);
    void createPurchaseOrderResponseReceived(const QJsonObject &response);
    void querySalesRecordsResponseReceived(const QJsonObject &response);
    void queryPurchaseRecordResponseReceived(const QJsonObject &response);
    void takeMedicineResponseReceived(const QJsonObject &response);

private slots:
    void onCheckExpiryReplyFinished();
    void onLoginReplyFinished();
    void onRegisterReplyFinished();
    void onDeleteUserReplyFinished();
    void onUpdateUserReplyFinished();
    void onAddMedicineReplyFinished();
    void onDeleteMedicineReplyFinished();
    void onEditMedicineReplyFinished();
    void onQueryMedicineByNameReplyFinished();
    void onQueryMedicineByCategoryReplyFinished();
    void onQueryMedicineByKeywordReplyFinished();
    void onCheckStockReplyFinished();
    void onCreatePurchaseOrderReplyFinished();
    void onQuerySalesRecordsReplyFinished();
    void onQueryPurchaseRecordReplyFinished();
    void onTakeMedicineReplyFinished();

private:
    QNetworkReply* sendRequest(RequestType, const QString&, const QJsonObject& = QJsonObject());
    QJsonDocument recvResponse(QNetworkReply *reply);

private:
    QNetworkAccessManager *networkAccessManager;
    QString domain;
};

#endif // NETWORKMANAGER_H
